package by.bsu.rfe.belianko.euniversity.controller.command;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import by.bsu.rfe.belianko.euniversity.constants.ErrorMessages;
import by.bsu.rfe.belianko.euniversity.constants.LoggerMessages;
import by.bsu.rfe.belianko.euniversity.constants.PageNames;
import by.bsu.rfe.belianko.euniversity.dao.IAdmissionDAO;
import by.bsu.rfe.belianko.euniversity.dao.IFacultiesDAO;
import by.bsu.rfe.belianko.euniversity.dao.IStudentsDAO;
import by.bsu.rfe.belianko.euniversity.exceptions.CommandException;
import by.bsu.rfe.belianko.euniversity.exceptions.DaoTechnicalException;
import by.bsu.rfe.belianko.euniversity.model.DataBaseLayer;
import by.bsu.rfe.belianko.euniversity.model.Faculty;
import by.bsu.rfe.belianko.euniversity.model.Student;

/**
 * Performs the complete receiving.
 * 
 * @author Vitali_Belianko
 * 
 */
public class CompleteCommand implements Command {
	private static final String CAN_NOT_COMPLETE_RECEIVING = "Can not complete receiving!";
	private static final Object CREATED_LIST_OF_ACCEPTED_STUDENTS = "Created list of accepted students.";
	private static final String ACCEPTED_STUDENT = "acceptedStudent";
	private static final String COMPLETE = "complete";
	private static final String ADMISSION_STATE = "admissionState";
	private static final IStudentsDAO studentsDao = DataBaseLayer.getStudentsDao();
	private static final IFacultiesDAO facultiesDao = DataBaseLayer.getFacultiesDAO();
	private static final IAdmissionDAO admissionDao = DataBaseLayer.getIAdmissionDAO();
	private static final Logger LOGGER = Logger.getLogger(CompleteCommand.class);

	/**
	 * Completes receiving of documents.Accepts students to the University.
	 * 
	 * @param request
	 * 
	 * @return admin page
	 * @throws CommandException
	 */
	@Override
	public String execute(HttpServletRequest request) throws CommandException {

		LOGGER.debug(LoggerMessages.STARTED_COMMAND);

		List<Faculty> facultyList = null;
		try {
			admissionDao.completeReceiving();
		} catch (DaoTechnicalException e) {
			LOGGER.error(e.getMessage());
			throw new CommandException(CAN_NOT_COMPLETE_RECEIVING, e);
		}

		LOGGER.debug("Complete receiving");

		try {
			facultyList = facultiesDao.getFacultyList();
		} catch (DaoTechnicalException e) {
			LOGGER.error(e.getMessage());
			throw new CommandException(ErrorMessages.CAN_NOT_GET_LIST_OF_FACULTY, e);
		}

		for (Faculty faculty : facultyList) {
			int plan = faculty.getPlan();
			long facultyId = faculty.getFacultyId();
			try {
				studentsDao.acceptStudents(facultyId, plan);
			} catch (DaoTechnicalException e) {
				LOGGER.error(e.getMessage());
				throw new CommandException(ErrorMessages.CAN_NOT_GET_FACULTY, e);
			}
		}

		List<Student> acceptedStudentsList = null;
		try {
			acceptedStudentsList = studentsDao.getAcceptedStudents();
		} catch (DaoTechnicalException e) {
			LOGGER.error(e.getMessage());
			throw new CommandException(ErrorMessages.CAN_NOT_GET_ACCEPTED_STUDENTS, e);
		}
		request.getSession(true).setAttribute(ACCEPTED_STUDENT, acceptedStudentsList);
		String admissionState = null;
		try {
			admissionState = admissionDao.getAdmissionState();
		} catch (DaoTechnicalException e) {
			LOGGER.error(e.getMessage());
			throw new CommandException(ErrorMessages.CAN_NOT_GET_ADMISSION_STATE, e);
		}

		LOGGER.debug(CREATED_LIST_OF_ACCEPTED_STUDENTS);

		if (COMPLETE.equals(admissionState)) {
			request.getSession(true).setAttribute(ADMISSION_STATE, admissionState);
		}

		LOGGER.debug(LoggerMessages.FINISHED_COMMAND);

		return PageNames.ADMIN_PAGE;
	}

}
